package com.example.yrdev.ordering_system.Main_Menu;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.example.yrdev.ordering_system.Common.CommonActivity;
import com.example.yrdev.ordering_system.Database.DingHanDB;
import com.example.yrdev.ordering_system.Model.CardRecord;
import com.example.yrdev.ordering_system.R;
import com.example.yrdev.ordering_system.Utils.GlobalFunction;
import com.example.yrdev.ordering_system.Utils.MIScanner;
import com.example.yrdev.ordering_system.homeOptions.PaymentDialog;

import java.util.ArrayList;
import java.util.TimerTask;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by luke on 10/11/2016.
 */

public class MenuCancelPurchase extends CommonActivity {

    @InjectView(R.id.content_container)SwipeMenuListView menuListView;
    public LayoutInflater inflater;
    public   ArrayList< CardRecord>cardRecordArrayList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initNFCParam();

        initView();

    }

    Context context;
 CardRecord cardRecord;
    CustomAdapter adapter;

    @Override
    public void initView() {

        super.initView();
        setBarTitle(GlobalFunction.getResourceString(this, R.string.cancel_sale));
        showBackButton();
        setContentView(R.layout.menu_cancel_purchase_layout);
        ButterKnife.inject(this);


         cardRecordArrayList   = DingHanDB.getInstance(this).getCardReport("",7);
         inflater=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);

     adapter  =new CustomAdapter();

        menuListView.setAdapter(adapter);
            context=MenuCancelPurchase.this;


        menuListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
          cardRecord    =   cardRecordArrayList.get(i);


          int currentIndex=DingHanDB.getInstance(context).getCardReportRefundIndex(cardRecord);

                if (currentIndex==0){
                    topUpCard();



                }else {
                    GlobalFunction.ToastAlert(context,"非常抱歉.您已退过款");
                }

            }
        });


    }


    PaymentDialog paymentDialog;

    public void topUpCard(){



            paymentDialog  =new PaymentDialog(MenuCancelPurchase.this,0,MenuCancelPurchase.this);
            paymentDialog.show();
            paymentDialog.changeTitle("取消扣款");
            paymentDialog.startPurchaseMode();
            paymentDialog.changePrice(String.valueOf(cardRecord.getPurchaseAmount()));
          //  paymentDialog.savePurchasePrice(cardRecord.getPurchaseAmount());

            reader.setOldCardID(cardRecord.getCardNo());
            reader.setReduceAmount(paymentDialog.getCurrentPrice());
            reader.setOperate(1);
            reader.setAction(0);
            reader.setPurchaseMode(3);
           // reader.setReduceAmount(cardRecord.getPurchaseAmount()*100);
            paymentDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                    paymentDialog=null;
                    reader.reset();//重启为只显示卡里详细信息
                }
            });

            reader.setCallback(new MIScanner.Callback() {
                @Override
                public void showFixPurchaseCost(Double cost) {

                }

                @Override
                public void getCardInfo(Double reduceAmount,Double remain,Double currentBalance) {
                 paymentDialog.showOtherNominal(reduceAmount,remain,currentBalance);

                    closeDialog();
                    DingHanDB.getInstance(MenuCancelPurchase.this).updateCardRefundIndex(cardRecord);
                  //  RefundGoodTB.insertRecord(context,refundItem);
                    GlobalFunction.ToastAlert(context,"退款成功");
                   adapter.notifyDataSetChanged();

                }

                @Override
                public void getMICardNo(String cardNo,String ticketID) {

                }
            });





    }

    public  void closeDialog(){

        new java.util.Timer().schedule(new TimerTask() {
            @Override
            public void run() {


                paymentDialog.dismiss();

                paymentDialog=null;

            }

        },1500);

    }


    //for reading MI CARD
    @Override
    protected void onResume() {
        super.onResume();

        try {
            reader.processIntent(mNfcAdapter,getIntent());
        }catch (Exception e){

        }

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        try {

            reader.processIntent(mNfcAdapter, intent);

        }catch (Exception e){

        }


    }

    private class CustomAdapter extends BaseAdapter{



        @Override
        public int getCount() {
            return cardRecordArrayList.size();
        }

        @Override
        public  CardRecord getItem(int i) {
            return cardRecordArrayList.get(i);

        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            if (view==null){
                view=inflater.inflate(R.layout.menu_cancel_purchase_row,null);

            }


            TextView cardNo=(TextView)view.findViewById(R.id.card_no);
            cardNo.setText(getItem(i).getCardNo());

            TextView cardUsageCount=(TextView)view.findViewById(R.id.card_usage_count);
            cardUsageCount.setText(String.valueOf(getItem(i).getUsageCount()));
//
            TextView card_name=(TextView)view.findViewById(R.id.card_name);
            card_name.setText(getItem(i).getCardName());
//
            TextView purch_time=(TextView)view.findViewById(R.id.purch_time);
            purch_time.setText(getItem(i).getUsageDate());
//
//
            TextView card_purchase_amount=(TextView)view.findViewById(R.id.card_purchase_amount);
            card_purchase_amount.setText(String.valueOf(getItem(i).getPurchaseAmount())+"元");

            TextView refunded=(TextView)view.findViewById(R.id.refunded);


            int currentIndex=DingHanDB.getInstance(MenuCancelPurchase.this).getCardReportRefundIndex(getItem(i));

            if (currentIndex!=0){

                refunded.setText("是");

            }else {

                refunded.setText("否");

            }
            return view;
        }
    }














}
